package com.yfs.cloud.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.github.pagehelper.PageInfo;
import com.yfs.cloud.authority.UserAuthority;
import com.yfs.cloud.commons.Constants;
import com.yfs.cloud.entity.Module;
import com.yfs.cloud.entity.Role;
import com.yfs.cloud.entity.User;
import com.yfs.cloud.mapper.UserMapper;
import com.yfs.cloud.service.RoleModuleService;
import com.yfs.cloud.service.UserRoleService;
import com.yfs.cloud.service.UserService;

/**
* @Company: 影法師工作室
* @Description: 用户服务
* @author 影法師  
* @date 2020年8月25日 上午7:07:53 
* @version V1.0
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements UserService {

	@Autowired
	private UserRoleService userRoleService;

	@Autowired
	private RoleModuleService roleModuleService;

	@Autowired
	private UserMapper userMapper;

	@Override
	public UserAuthority findUserAuthority(Long id) {
		UserAuthority userAuthority = new UserAuthority();
		// 根据 id 获取用户拥有的角色
		List<Role> roles = userRoleService.findRolesByUserId(id);
		// 角色 id
		List<Long> roleIds = new ArrayList<>();
		// 角色名称列表
		List<String> rolesString = new ArrayList<>();
		roles.forEach(role -> {
			rolesString.add(role.getName());
			roleIds.add(role.getId());
		});
		// 填充用户id
		userAuthority.setUserId(id);
		// 填充角色
		userAuthority.setRoles(rolesString);
		// 根据角色 id 查询相应的模块权限
		List<Module> modules = roleModuleService.findModulesByRoleIds(roleIds);
		List<String> moduleString = new ArrayList<>();
		modules.forEach(module -> {
			moduleString.add(module.getName());
		});
		// 填充模块权限
		userAuthority.setModules(moduleString);
		return userAuthority;
	}

	@Transactional
	@Override
	public void modifyLeadByBindPhone(String phone, String username) throws Exception {
		// 根据账号查找唯一的用户
		User userQueryByUsername = new User();
		userQueryByUsername.setUsername(username);
		User userInDBByUserName = super.findOne(userQueryByUsername);
		// 如果发现用户不存在的话, 则抛异常
		Assert.notNull(userInDBByUserName, Constants.USER_NOT_EXIST);
		// 如果已经完成了绑定的话, 则直接返回
		if (StringUtils.isNotBlank(userInDBByUserName.getPhone())) {
			return;
		}
		// 根据手机号查找唯一的用户
		User userQueryByPhone = new User();
		userQueryByPhone.setPhone(phone);
		User userInDBByPhone = super.findOne(userQueryByPhone);
		// 如果发现用户不存在的话, 则抛异常
		Assert.notNull(userInDBByPhone, Constants.USER_NOT_EXIST);
		// 先物理删除老的数据
		super.remove(userInDBByPhone.getId());
		// 再更新新的数据
		userInDBByUserName.setPhone(phone);
		super.modify(userInDBByUserName);
	}

	@Override
	public void modifyLeadByBindWx(String openid, String username) throws Exception {
		// 根据账号查找唯一的用户
		User userQueryByUsername = new User();
		userQueryByUsername.setUsername(username);
		User userInDBByUserName = super.findOne(userQueryByUsername);
		// 如果发现用户不存在的话, 则抛异常
		Assert.notNull(userInDBByUserName, Constants.USER_NOT_EXIST);
		// 如果已经完成了绑定的话, 则直接返回
		if (StringUtils.isNotBlank(userInDBByUserName.getPhone())) {
			return;
		}
		// 根据微信openid查找唯一的用户
		User userQueryByWx = new User();
		userQueryByWx.setOpenId(openid);
		User userInDBByWx = super.findOne(userQueryByWx);
		// 如果发现用户不存在的话, 则抛异常
		Assert.notNull(userInDBByWx, Constants.USER_NOT_EXIST);
		// 先物理删除老的数据
		super.remove(userInDBByWx.getId());
		// 再更新新的数据
		userInDBByUserName.setOpenId(openid);
		super.modify(userInDBByUserName);
	}

	@Override
	public PageInfo<Map<String, String>> findPageUserByRoleIdAndWxNickname(Map<String, Object> params) {
		List<Map<String, String>> list = userMapper.selectPageUserByRoleIdAndWxNickname(params);
		return new PageInfo<>(list);
	}

	@Override
	public User findOneById(Long id) {
		return userMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<User> findListByRoleIds(Map<String, Object> params) {
		return userMapper.selectListByRoleIds(params);
	}

}
